<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class CaseDesignerService extends BaseService
{
    //设计师

    //选项
    public function getOption()
    {
        $arys = [];
        $cateOnes = $this->dbConn($this->dbCate)
            ->where('del', 0)
            ->where('type', 2)
            ->get();
        if (count($cateOnes)) {
            foreach ($cateOnes as $v) {
                $v = (array) $v;
                $arys['cate'][] = [
                    'id' => $v['id'],
                    'title' => $v['title'],
                ];
            }
        }
        return $arys;
    }

    //列表
    public function getIndex($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbDesigner)->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $cateOne = (array) $this->dbConn($this->dbCate)
                ->where('id', $v['cate_id'])
                ->first();
            $comOne = (array) $this->dbConn($this->dbCompany)
                ->where('id', $v['cid'])
                ->first();
            $arys[] = [
                'id' => $v['id'],
                'realname' => $v['realname'],
                'head' => file_exists($v['head']) ? '/'.$v['head'] : '',
                'work_year' => $v['work_year'] ? $v['work_year'] : '-',
                'cate_id' => $v['cate_id'],
                'cate_name' => empty($cateOne) ? '-' : $cateOne['title'],
                'score' => $v['score'],
                'cid' => $v['cid'],
                'cname' => empty($comOne) ? '-' : $comOne['cname'],
                'uptime' => $v['uptime'] ? $v['uptime'] : '-',
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    //详情
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbDesigner)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $cateOne = (array) $this->dbConn($this->dbCate)
            ->where('id', $res['cate_id'])
            ->first();
        $comOne = (array) $this->dbConn($this->dbCompany)
            ->where('id', $res['cid'])
            ->first();
        $arys = [
            'id' => $res['id'],
            'realname' => $res['realname'],
            'prefix' => file_exists($res['head']) ? '/' : '',
            'head' => file_exists($res['head']) ? $res['head'] : '',
            'work_year' => $res['work_year'] ? $res['work_year'] : '',
            'cate_id' => $res['cate_id'],
            'cate_name' => empty($cateOne) ? '-' : $cateOne['title'],
            'score' => $res['score'],
            'cid' => $res['cid'],
            'cname' => empty($comOne) ? '' : $comOne['cname'],
            'uptime' => $res['uptime'] ? $res['uptime'] : '',
        ];
        return $this->toback(0, '', $arys);
    }

    //添加
    public function add($pas)
    {
        $res = (array) $this->dbConn($this->dbDesigner)
            ->where('del', 0)
            ->where('realname', $pas['realname'])
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '存在同名公司');
        }
        $pas['uptime'] = $pas['addtime'] = toDate();
        $lastid = $this->dbConn($this->dbDesigner)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, '添加失败');
        }
        return $this->toback();
    }

    //修改
    public function modify($id, $pas)
    {
        $res = (array) $this->dbConn($this->dbDesigner)->where('id', $id)->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbDesigner)->where('id', $id)->update($pas);
        if (!$up) {
            return $this->toback(-1, '修改失败');
        }
        return $this->toback();
    }

    //删除
    public function del($id)
    {
        $res = (array) $this->dbConn($this->dbDesigner)->where('id', $id)->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $up = $this->dbConn($this->dbDesigner)->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, '删除失败');
        }
        return $this->toback();
    }
}